//
// Created by 卞证 on 2021/05/20.
//
#pragma once

#include <vector>
#include <fstream>
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
#include "omp.h"

namespace Diversity {

    std::vector<int> load_partition_txt(const char *filename) {

        std::vector<int> partition_l;
        std::ifstream instream;
        instream.open(filename, std::ios::in);
        if (!instream.is_open()) {//判断文件是否打开
            std::cout << "Error opening file" << std::endl;
        }
        std::string line;

        while (true) {
            getline(instream, line);
            if (instream.eof()) {
                break;
            }
            int num = std::atoi(line.c_str());
            partition_l.push_back(num);
        }
        return partition_l;
    }

    std::vector<std::set<int>> get_label_map(std::vector<int> &partition_l, int k) {
        std::vector<std::set<int>> label_map(k);
        int n_item = partition_l.size();
        for (int i = 0; i < n_item; i++) {
            int cluster_idx = partition_l[i];
            label_map[cluster_idx].insert(i);
        }
        return label_map;
    }

    int union_size(std::set<int> &a, std::set<int> &b) {
        std::set<int> res;
        std::set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(res, res.begin()));
        int size = res.size();
        return size;
    }

    int intersection_size(std::set<int> &a, std::set<int> &b) {
        std::set<int> res;
        std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), inserter(res, res.begin()));
        int size = res.size();
        return size;
    }
}